package com.strelitzia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.strelitzia.dao.FriendDao;
import com.strelitzia.entity.*;
import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.filter.CorsFilter;
import com.strelitzia.service.FriendsService;
import com.strelitzia.service.NotificationService;
import com.strelitzia.service.UserService;
import com.strelitzia.utils.FileUtil;
import com.strelitzia.utils.TimeUtil;
import com.strelitzia.vo.ChatContextVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @create 2023/12/18 10:56
 */
@Service
public class FriendsServiceImpl extends ServiceImpl<FriendDao, FriendsEntity> implements FriendsService {
    @Autowired
    private UserService userService;

    @Autowired
    private NotificationService notificationService;

    @Override
    public List<FriendItem> getFriends() {
        return this.baseMapper.getList(CorsFilter.loginUserId.get());
    }

    @Override
    public ChatContextVo getChatHistory(Integer friendId) throws StrelitziaException {
        if (!(friendId == null || friendId == 0)) {
            List<ChatContext> chatHistory = FileUtil.getChatHistory(CorsFilter.loginUserId.get(), friendId);
            if (chatHistory != null) {
                ChatContextVo chatContextVo = new ChatContextVo();
                chatContextVo.setChatContexts(chatHistory);
                chatContextVo.setUserAvatar("/api/res/" + userService.getById(CorsFilter.loginUserId.get()).getAvatar());
                UserEntity byId = userService.getById(friendId);
                chatContextVo.setFriendAvatar("/api/res/" + byId.getAvatar());
                FriendsEntity one = this.getOne(new QueryWrapper<FriendsEntity>()
                        .eq("uid", CorsFilter.loginUserId.get())
                        .eq("friend_id", friendId));
                FriendsEntity two = this.getOne(new QueryWrapper<FriendsEntity>()
                        .eq("uid", friendId)
                        .eq("friend_id", CorsFilter.loginUserId.get()));
                if (one != null && !"".equals(one.getRemark())) {
                    chatContextVo.setRemark(one.getRemark());
                } else {
                    chatContextVo.setRemark(byId.getNickname());
                }
                if (one == null && two != null) {
                    chatContextVo.setFollower(friendId);
                }
                if (two == null && one != null) {
                    chatContextVo.setFollower(CorsFilter.loginUserId.get());
                }
                chatContextVo.setIsFriend(one != null && two != null);
                return chatContextVo;
            }
        }
        throw new StrelitziaException("获取好友列表失败");
    }

    @Override
    public void changeRemarks(Integer friendId, String remark) {
        FriendsEntity friendsEntity = new FriendsEntity();
        friendsEntity.setRemark(remark);
        this.update(friendsEntity, new QueryWrapper<FriendsEntity>()
                .eq("uid", CorsFilter.loginUserId.get())
                .eq("friend_id", friendId));
    }

    @Override
    public void cacheFollow(Integer friendId) {
        this.remove(new QueryWrapper<FriendsEntity>()
                .eq("uid", CorsFilter.loginUserId.get())
                .eq("friend_id", friendId));
    }

    @Override
    public Integer getFans() {
        return this.count(new QueryWrapper<FriendsEntity>().eq("friend_id", CorsFilter.loginUserId.get()));
    }

    @Override
    public Integer getFollow() {
        return this.count(new QueryWrapper<FriendsEntity>().eq("uid", CorsFilter.loginUserId.get()));
    }

    @Override
    public Integer getFans(Integer id) {
        return this.count(new QueryWrapper<FriendsEntity>().eq("friend_id", id));
    }

    @Override
    public Integer getFollow(Integer id) {
        return this.count(new QueryWrapper<FriendsEntity>().eq("uid", id));
    }

}
